export const state = () => {
    return {
        data: [],
        totals: 0,
        randlist: [],
        num: 0
    }
}
export const mutations = {
    SET_LIST(state, v) {
        state.data = v.data
        state.totals = v.total
    },
    SET_RANDLIST(state, v) {
        state.randlist = v
    },
    SET_NUM(state, v) {
        state.num = v
    }

}

export const actions = {
    async getData({ commit }) {
        const { data } = await this.$axios.get('http://localhost:3001/questions/exam-list?pageSize=10&sortBy=true')
        console.log(data)
        commit('SET_LIST', data)
    },
    async addData({ }, row) {
        console.log(row)
        row.answers = JSON.stringify(row.answers)
        row.options = JSON.stringify(row.options)
        const r = await this.$axios.post('http://localhost:3001/questions/save-exam', row)
        console.log(r)
    },
    async editData({ }, row) {
        console.log(row)
        let r = await this.$axios.post('http://localhost:3001/questions/update-exam', row)
        console.log(r)
    },
    async delData({ }, row) {
        console.log(row)
        await this.$axios.get(`http://localhost:3001/questions/delete-exam?id=${row}`)
    },
    async getRanData({ commit }) {
        const { data } = await this.$axios.get('http://localhost:3001/questions/rand-list?pageSize=20')
        data.data.forEach(item => {
            item.options = JSON.parse(item.options);
        });
        commit('SET_RANDLIST', data)
    }
}

export const getters = {

}